package javaStudy.反射;

import javaStudy.io流.字符流;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/*
通过反射获取成员方法
 */
public class Demo2 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<Student> studentClass = Student.class;

        //获取student里面的method方法
        Method method = studentClass.getMethod("method");
        //获得构造方法并创建对象
        Constructor<Student> constructor = studentClass.getConstructor();
        Student student = constructor.newInstance();
        //调用student无参数的method方法
        method.invoke(student);

        //调用student有参数的method方法
        Method method1 = studentClass.getMethod("method1", String.class, int.class);
        method1.invoke(student,"hu",20);

    }
}
